﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
using System.Windows.Forms;
using System.Collections.Specialized;

namespace AjaxSnippets.Design
{
    public class MapParametersWizardStep : WizardStep
    {
        private ConfigureSnippetCallbackWizard _Wizard;
        private System.Windows.Forms.DataGridView MappingGridView;
        private DataGridViewTextBoxColumn ParameterColumn;
        private DataGridViewComboBoxColumn ControlIDColumn;
        private MethodInfo _MappedWebMethod = null;
        private OrderedDictionary _ComboBoxItemsDict = new OrderedDictionary();
        public MapParametersWizardStep(ConfigureSnippetCallbackWizard wizard)
        {
            _Wizard = wizard;

            InitializeComponent();

            try
            {
                DataGridViewComboBoxColumn c = (DataGridViewComboBoxColumn)MappingGridView.Columns["ControlIDColumn"];
                c.ValueMember = "Value";
                c.DisplayMember = "DisplayText";

                c.Items.Add("Choose");
                _ComboBoxItemsDict.Add("Choose", "Choose");
                
                foreach (string controlId in GetWebControlIds())
                {
                    _ComboBoxItemsDict.Add(controlId, controlId);
                    c.Items.Add(controlId);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }


        public override string StepTitle
        {
            get { return "Map web method parameters"; }
        }

        public override bool CanFinish
        {
            get { return true; }
        }

        private void InitializeComponent()
        {
            this.MappingGridView = new System.Windows.Forms.DataGridView();
            this.ParameterColumn = new System.Windows.Forms.DataGridViewTextBoxColumn();
            this.ControlIDColumn = new System.Windows.Forms.DataGridViewComboBoxColumn();
            ((System.ComponentModel.ISupportInitialize)(this.MappingGridView)).BeginInit();
            this.SuspendLayout();
            // 
            // MappingGridView
            // 
            this.MappingGridView.AllowUserToAddRows = false;
            this.MappingGridView.AllowUserToDeleteRows = false;
            this.MappingGridView.AllowUserToResizeRows = false;
            this.MappingGridView.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
            this.MappingGridView.Columns.AddRange(new System.Windows.Forms.DataGridViewColumn[] {
            this.ParameterColumn,
            this.ControlIDColumn});
            this.MappingGridView.Dock = System.Windows.Forms.DockStyle.Top;
            this.MappingGridView.Location = new System.Drawing.Point(0, 0);
            this.MappingGridView.MultiSelect = false;
            this.MappingGridView.Name = "MappingGridView";
            this.MappingGridView.RowHeadersVisible = false;
            this.MappingGridView.ShowEditingIcon = false;
            this.MappingGridView.Size = new System.Drawing.Size(632, 260);
            this.MappingGridView.TabIndex = 0;
            // 
            // ParameterColumn
            // 
            this.ParameterColumn.AutoSizeMode = System.Windows.Forms.DataGridViewAutoSizeColumnMode.Fill;
            this.ParameterColumn.HeaderText = "Parameter";
            this.ParameterColumn.Name = "ParameterColumn";
            this.ParameterColumn.ReadOnly = true;
            // 
            // ControlIDColumn
            // 
            this.ControlIDColumn.AutoSizeMode = System.Windows.Forms.DataGridViewAutoSizeColumnMode.Fill;
            this.ControlIDColumn.DisplayStyle = System.Windows.Forms.DataGridViewComboBoxDisplayStyle.ComboBox;
            this.ControlIDColumn.HeaderText = "Control ID";
            this.ControlIDColumn.Name = "ControlIDColumn";
            // 
            // MapParametersWizardStep
            // 
            this.Controls.Add(this.MappingGridView);
            this.Name = "MapParametersWizardStep";
            this.Size = new System.Drawing.Size(632, 282);
            ((System.ComponentModel.ISupportInitialize)(this.MappingGridView)).EndInit();
            this.ResumeLayout(false);

        }

        public override void OnActive()
        {
            if (_MappedWebMethod != null)
            {
                // This is a newly selected web method so clear these mappings
                if (_MappedWebMethod != _Wizard.SelectedWebMethod)
                {
                    this.MappingGridView.Rows.Clear();
                    _MappedWebMethod = _Wizard.SelectedWebMethod;
                }
            }
            else
            {
                _MappedWebMethod = _Wizard.SelectedWebMethod;
            }

            if (this.MappingGridView.Rows.Count == 0)
            {
                ParameterInfo[] parameters = _MappedWebMethod.GetParameters();
                DataGridViewComboBoxColumn controlIdColumn = (DataGridViewComboBoxColumn)MappingGridView.Columns["ControlIDColumn"];

                foreach (ParameterInfo parm in parameters)
                {
                    int selectedIdx = 0;
                    // Try to best guess the mapping
                    if (_ComboBoxItemsDict[parm.Name] != null && controlIdColumn.Items.Contains(_ComboBoxItemsDict[parm.Name]))
                    {
                        selectedIdx = controlIdColumn.Items.IndexOf(_ComboBoxItemsDict[parm.Name]);
                    }

                    this.MappingGridView.Rows.Add(parm.Name, controlIdColumn.Items[selectedIdx]);

                }
            }


        }

        public override void OnNext()
        {
            SetWizardParameterMappings();
        }

        public override void OnPrevious()
        {
            SetWizardParameterMappings();
        }

        public override void OnFinish()
        {
            SetWizardParameterMappings();
        }
        private void SetWizardParameterMappings()
        {
            _Wizard.ParameterMapping.Clear();  
            foreach (DataGridViewRow row in MappingGridView.Rows)
            {
                if (row.Cells[1].Value != _ComboBoxItemsDict[0])
                    _Wizard.ParameterMapping.Add(row.Cells[0].Value.ToString(), row.Cells[1].Value.ToString());
            }
        }

        private List<string> GetWebControlIds()
        {
            List<string> controlIds = new List<string>();

            if (_Wizard.DesignerHost.RootComponent is System.Web.UI.Control)
            {
                System.Web.UI.Control hostWebControl = (System.Web.UI.Control)_Wizard.DesignerHost.RootComponent;

                foreach (System.Web.UI.Control childControl in hostWebControl.Controls)
                {
                    if (!String.IsNullOrEmpty(childControl.ID))
                        controlIds.Add(childControl.ID);
                }

            }
            return controlIds;

        }
    }
}
